package com.caiyi.financial.nirvana.sms.service;

import com.caiyi.financial.nirvana.sms.base.service.BaseService;
import com.caiyi.financial.nirvana.sms.entity.SmsKeyword;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.mapper.SmsKeywordMapper;
import com.caiyi.financial.nirvana.sms.querybean.PageBean;
import com.caiyi.financial.nirvana.sms.querybean.QueryKeywordBean;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SmsKeywordService extends BaseService<SmsKeyword> {

    @Autowired
    private SmsKeywordMapper smsKeywordMapper;

    public int addKeyword(SmsKeyword smsKeyword) {
        int insertCount = this.insertByEntitySelective(smsKeyword);
        //redis中添加keyword
        String key = String.format(JedisKey.SMS_KEYWORD);
        JedisService.sadd(key, smsKeyword.getContent());
        return insertCount;
    }

    public int deleteKeyword(Integer id) {
        SmsKeyword smsKeyword = new SmsKeyword();
        smsKeyword.setId(id);
        smsKeyword = this.selectByPrimaryKey(smsKeyword);
        int deleteCount = this.deleteByPrimaryKey(smsKeyword);
        //删除redis中keyword
        JedisService.srem(JedisKey.SMS_KEYWORD, smsKeyword.getContent());
        logger.info("deleteByKeywordId id {}", id);
        return deleteCount;
    }

    public int updateKeyword(SmsKeyword smsKeyword) {
        //删除redis队列中原来keyword
        SmsKeyword keyword = new SmsKeyword();
        keyword.setId(smsKeyword.getId());
        keyword = this.selectByPrimaryKey(keyword);
        JedisService.srem(JedisKey.SMS_KEYWORD, keyword.getContent());
        int updateCount = this.updateByEntitySelective(smsKeyword);
        //更新redis中关键词
        JedisService.sadd(JedisKey.SMS_KEYWORD, smsKeyword.getContent());
        logger.info("updateByKeywordId id {}", smsKeyword.getId());
        return updateCount;
    }

    public PageBean<SmsKeyword> getKeywordByParams(QueryKeywordBean bean) {
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        List<SmsKeyword> list = smsKeywordMapper.selectKeywordListByParams(bean);
        PageBean<SmsKeyword> pageBean = new PageBean<>(list);
        return pageBean;
    }

}
